#!/usr/bin/env python
from utils.database import SQLAlchemy

db = SQLAlchemy('mysql://root:123456@localhost/happypress?charset=utf8')

import datetime

class User(db.Base):
    __tablename__ = 'blog_users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(128), nullable=False)
    email = db.Column(db.String(128), unique=True)
    password = db.Column(db.String(128))
    is_active = db.Column(db.Boolean, default=True)
    is_superuser = db.Column(db.Boolean, default=False)
    created_time = db.Column(db.DateTime)
    last_login = db.Column(db.DateTime) # nullable=True
    # profile
    profile = db.Column(db.Text)
    
    entries = db.relation('Entry', backref='blog_users')

    def __init__(self, username, email, password, is_superuser=False):
        self.username = username
        self.email = email
        self.password = password
        self.is_active = True
        self.is_superuser = is_superuser
        self.created_time = datetime.datetime.now()
        self.last_login = datetime.datetime.now()

    def __repr__(self):
        return "<User('%s','%s','%s','%s','%s')>" % (self.username, self.email, self.password, self.created_time, self.last_login)

entry_categories = db.Table('blog_entry_categories', db.Base.metadata,
    db.Column('category_id', db.Integer, db.ForeignKey('blog_categories.id')),
    db.Column('entry_id', db.Integer, db.ForeignKey('blog_entries.id'))
)

class Category(db.Base):
    __tablename__ = 'blog_categories'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(128), nullable=False)
    slug = db.Column(db.String(128), nullable=False)
    sort_order = db.Column(db.Integer, default=99)
    
    entries = db.relation('Entry', secondary=entry_categories, backref='blog_categories')

    def __init__(self, title, slug, sort_order=99):
        self.title = title
        self.slug = slug
        self.sort_order = sort_order

    def __repr__(self):
        return "<Category('%s','%s')>" % (self.title, self.slug)

class Entry(db.Base):
    __tablename__ = 'blog_entries'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(128), nullable=False)
    slug = db.Column(db.String(128), nullable=False)
    excerpt = db.Column(db.String(128))
    body = db.Column(db.Text, nullable=False)
    pub_date = db.Column(db.DateTime)
    up_date = db.Column(db.DateTime)
    categories = db.relation('Category', secondary=entry_categories, backref='blog_entries')
    author_id = db.Column(db.Integer, db.ForeignKey('blog_users.id'))
    enable_comments = db.Column(db.Boolean, default=True)
    featured = db.Column(db.Boolean, default=False)
    status = db.Column(db.String(12), default='live') # live,draft    
    comments = db.relation('Comment', backref='blog_entries')

    def __init__(self, title, slug, excerpt, body, author_id, enable_comments, featured, status='live', pub_date=datetime.datetime.now(),up_date=datetime.datetime.now()):
        self.title = title
        self.slug = slug
        self.excerpt = excerpt
        self.body = body
        self.author_id = author_id
        self.enable_comments = enable_comments
        self.featured = featured
        self.status = status
        self.pub_date = pub_date
        self.up_date = up_date

    def __repr__(self):
        return "<Entry('%s','%s','%s')>" % (self.title, self.pub_date, self.author_id)

class Comment(db.Base):
    __tablename__ = 'blog_comments'
    
    id = db.Column(db.Integer, primary_key=True)
    entry_id = db.Column(db.Integer, db.ForeignKey('blog_entries.id'))
    name = db.Column(db.String(128), nullable=False)
    email = db.Column(db.String(128))
    website = db.Column(db.String(128))
    body = db.Column(db.Text, nullable=False)
    ip = db.Column(db.String(128))
    pub_date = db.Column(db.DateTime)
    
    def __init__(self, name, body, email=None, website=None):
        self.body = body
        self.name = name
        self.email = email
        self.website = website
        self.pub_date = datetime.datetime.now()
        
    def __repr__(self):
        return "<Comment('%s','%s','%s')>" % (self.name, self.body, self.pub_date)

class Link(db.Base):
    __tablename__ = 'blog_links'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128), nullable=False)
    url = db.Column(db.String(128))
    alt = db.Column(db.String(128))
    sort_order = db.Column(db.Integer, default=99)
    
    def __init__(self, name, url, alt=None, sort_order=99):
        self.name = name
        self.url = url
        self.alt =  alt
        self.sort_order = sort_order
        
    def __repr__(self):
        return "<Link('%s','%s','%s','%s')>" % (self.name, self.url, self.alt, self.sort_order)

class Upload(db.Base):
    __tablename__ = 'blog_uploads'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('blog_users.id'))
    fileurl = db.Column(db.String(128), nullable=False)
    used_times = db.Column(db.Integer, default=0)
    upload_time = db.Column(db.DateTime)
    
    def __init__(self, fileurl, user_id, used_times=0):
        self.fileurl = fileurl
        self.user_id = user_id
        self.used_times = used_times
        self.upload_time = datetime.datetime.now()
        
    def __repr__(self):
        return "<Upload('%s','%s','%s')>" % (self.fileurl, self.user_id, self.used_times)

class Theme(db.Base):
    __tablename__ = 'blog_themes'
    
    id = db.Column(db.Integer, primary_key=True)
    prefix = db.Column(db.String(128), nullable=False)
    name = db.Column(db.String(128))
    info = db.Column(db.Text)
    
    def __init__(self, prefix, name, info=None):
        self.prefix =prefix
        self.name = name
        self.info = info
        
    def __repr__(self):
        return "<Themes('%s','%s')>" % (self.prefix, self.name)

class Setting(db.Base):
    __tablename__ = 'blog_settings'
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(128), nullable=False)
    subtitle = db.Column(db.String(128))
    theme_id = db.Column(db.Integer, db.ForeignKey('blog_themes.id'))
    
    themes = db.relation('Theme', backref='blog_settings')
    
    def __init__(self, theme_id, title, subtitle=None):
        self.theme_id = theme_id
        self.title = title
        self.subtitle = subtitle
        
    def __repr__(self):
        return "<Sites('%s','%s')>" % (self.title, self.subtitle)


if __name__=='__main__':
    print """
        use follow cmd to create database:
        $python setup.py
    """
